<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%prize}}".
 *
 * @property string $prizeId
 * @property string $title
 * @property string $desc
 * @property string $image
 * @property integer $chance
 */
class Prize extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%prize}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'desc', 'image', 'chance'], 'required'],
            [['chance'], 'integer'],
            [['title', 'desc'], 'string', 'max' => 20],
            [['image'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'prizeId' => 'Prize ID',
            'title' => '奖品名称',
            'desc' => 'Desc',
            'image' => '奖品图片',
            'chance' => '中奖概率',
        ];
    }

    public function beforeValidate()
    {
        if ($this->chance > 100 || $this->chance < 0) {
            $this->addError('chance', '中奖概率介于0-100之间');
            return false;
        }
        return parent::beforeValidate();
    }


}
